package com.erp.erpboot.controller.base;

import com.alibaba.fastjson.JSON;
import com.erp.erpboot.factory.ApplicationContextFactory;
import com.erp.erpboot.mapper.WarehouseMapper;
import com.erp.erpboot.pojo.DataObject;
import com.erp.erpboot.pojo.Warehouse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/warehouse")
public class WarehouseController {
    @Autowired
    WarehouseMapper warehouseMapper;

    ApplicationContext context = ApplicationContextFactory.getApplicationContext();

    @RequestMapping({"/", "/index"})
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ModelAndView mav = new ModelAndView("warehouse/index");
        return mav;
    }

    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public ModelAndView create(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ModelAndView mav = new ModelAndView("warehouse/create");
        return mav;
    }

    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public DataObject create(Warehouse warehouse) throws Exception {
        warehouseMapper.add(warehouse);

        DataObject dataObject = (DataObject) context.getBean("dataObject");

        return dataObject;
    }

    @RequestMapping(value = {"/update"}, method = RequestMethod.GET)
    public ModelAndView update(int id) throws Exception {
        ModelAndView mav = new ModelAndView("warehouse/update");
        Warehouse warehouse = warehouseMapper.get(id);
        mav.addObject("data", JSON.toJSONString(warehouse));
        return mav;
    }

    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public DataObject update(Warehouse warehouse) throws Exception {
        warehouseMapper.update(warehouse);

        DataObject dataObject = (DataObject) context.getBean("dataObject");

        return dataObject;
    }

    @ResponseBody
    @RequestMapping(value = "/deleteList", method = RequestMethod.POST)
    public DataObject deleteList(@RequestBody List<Integer> ids) throws Exception {
        warehouseMapper.deleteList(ids);

        DataObject dataObject = (DataObject) context.getBean("dataObject");

        return dataObject;
    }

    @ResponseBody
    @RequestMapping("/getListWithPage")
    public String getListWithPage(int page, int limit) throws Exception {

        DataObject dataObject = (DataObject) context.getBean("dataObject");
        List<Warehouse> data = warehouseMapper.listByPage(limit, page);
        dataObject.setData(data);
        dataObject.setCount(warehouseMapper.count());
        return JSON.toJSONString(dataObject);
    }
}
